<!--
 * @Descripttion: ( 物料规则配置/material_rule_config)
 * @Author: (admin)
 * @Date: (2024-09-29)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['materialruleconfig:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange"
      >
      <el-table-column prop="id" label="序号" align="center" v-if="columns.showColumn('id')"/>
      <el-table-column prop="lineCode" label="产线代号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('lineCode')"/>
      <el-table-column prop="stationCode" label="工位代号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('stationCode')"/>
      <el-table-column prop="materialName" label="物料名称" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('materialName')"/>
      <el-table-column prop="materialCode" label="物料号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('materialCode')"/>
      <el-table-column prop="drawingNumber" label="零件图号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('drawingNumber')"/>
      <el-table-column prop="orderCode" label="订单号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('orderCode')"/>
      <el-table-column prop="productType" label="产品类型" align="center" v-if="columns.showColumn('productType')">
        <template #default="scope">
          <dict-tag :options=" options.productTypeOptions" :value="scope.row.productType"  />
        </template>
      </el-table-column>
      <el-table-column prop="startIndex" label="起始索引（用于解析规则）" align="center" v-if="columns.showColumn('startIndex')"/>
      <el-table-column prop="endIndex" label="结束索引（用于解析规则）" align="center" v-if="columns.showColumn('endIndex')"/>
      <el-table-column prop="productionBatchNum" label="生产批次号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('productionBatchNum')"/>
      <el-table-column prop="supplierName" label="供应商名称" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('supplierName')"/>
      <el-table-column prop="totalCount" label="总数" align="center" v-if="columns.showColumn('totalCount')"/>
      <el-table-column prop="finishedCount" label="消耗数量" align="center" v-if="columns.showColumn('finishedCount')"/>
      <el-table-column prop="noteInfo" label="备注" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('noteInfo')"/>
      <el-table-column prop="bomVersion" label="BOM版本" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('bomVersion')"/>
      <el-table-column prop="creationTime" label="创建时间" :show-overflow-tooltip="true"  v-if="columns.showColumn('creationTime')"/>
      <el-table-column prop="creatorName" label="创建人" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('creatorName')"/>
      <el-table-column prop="isEnabled" label="软删除（1：启用，0：弃用）" align="center" v-if="columns.showColumn('isEnabled')">
        <template #default="scope">
          <dict-tag :options=" options.isEnabledOptions" :value="scope.row.isEnabled"  />
        </template>
      </el-table-column>
      <el-table-column prop="spare1" label="预留" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('spare1')"/>
      <el-table-column prop="spare2" label="预留" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('spare2')"/>
      <el-table-column prop="spare3" label="预留" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('spare3')"/>
      <el-table-column prop="spare4" label="预留" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('spare4')"/>
      <el-table-column prop="spare5" label="预留" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('spare5')"/>
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['materialruleconfig:edit']" @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['materialruleconfig:delete']" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />


    <!-- 添加或修改 物料规则配置对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open" >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
            
          <el-col :lg="12">
            <el-form-item label="序号" prop="id">
              <el-input v-model.number="form.id" placeholder="请输入序号" :disabled="opertype != 1"/>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="产线代号" prop="lineCode">
              <el-input v-model="form.lineCode" placeholder="请输入产线代号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="工位代号" prop="stationCode">
              <el-input v-model="form.stationCode" placeholder="请输入工位代号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="请输入物料名称" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="物料号" prop="materialCode">
              <el-input v-model="form.materialCode" placeholder="请输入物料号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="零件图号" prop="drawingNumber">
              <el-input v-model="form.drawingNumber" placeholder="请输入零件图号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="订单号" prop="orderCode">
              <el-input v-model="form.orderCode" placeholder="请输入订单号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="产品类型" prop="productType">
              <el-select v-model="form.productType"  placeholder="请选择产品类型">
                <el-option
                  v-for="item in options.productTypeOptions" 
                  :key="item.dictValue" 
                  :label="item.dictLabel" 
                  :value="item.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
            
          <el-col :lg="12">
            <el-form-item label="起始索引（用于解析规则）" prop="startIndex">
              <el-input v-model.number="form.startIndex" placeholder="请输入起始索引（用于解析规则）" />
            </el-form-item>
          </el-col>
            
          <el-col :lg="12">
            <el-form-item label="结束索引（用于解析规则）" prop="endIndex">
              <el-input v-model.number="form.endIndex" placeholder="请输入结束索引（用于解析规则）" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="生产批次号" prop="productionBatchNum">
              <el-input v-model="form.productionBatchNum" placeholder="请输入生产批次号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="供应商名称" prop="supplierName">
              <el-input v-model="form.supplierName" placeholder="请输入供应商名称" />
            </el-form-item>
          </el-col>
            
          <el-col :lg="12">
            <el-form-item label="总数" prop="totalCount">
              <el-input v-model.number="form.totalCount" placeholder="请输入总数" />
            </el-form-item>
          </el-col>
            
          <el-col :lg="12">
            <el-form-item label="消耗数量" prop="finishedCount">
              <el-input v-model.number="form.finishedCount" placeholder="请输入消耗数量" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="备注" prop="noteInfo">
              <el-input v-model="form.noteInfo" placeholder="请输入备注" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="BOM版本" prop="bomVersion">
              <el-input v-model="form.bomVersion" placeholder="请输入BOM版本" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建时间" prop="creationTime">
              <el-date-picker v-model="form.creationTime" type="datetime" :teleported="false" placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建人" prop="creatorName">
              <el-input v-model="form.creatorName" placeholder="请输入创建人" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="软删除（1：启用，0：弃用）" prop="isEnabled">
              <el-radio-group v-model="form.isEnabled">
                <el-radio v-for="item in options.isEnabledOptions" :key="item.dictValue" :value="item.dictValue">
                  {{item.dictLabel}}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="预留" prop="spare1">
              <el-input v-model="form.spare1" placeholder="请输入预留" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="预留" prop="spare2">
              <el-input v-model="form.spare2" placeholder="请输入预留" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="预留" prop="spare3">
              <el-input v-model="form.spare3" placeholder="请输入预留" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="预留" prop="spare4">
              <el-input v-model="form.spare4" placeholder="请输入预留" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="预留" prop="spare5">
              <el-input v-model="form.spare5" placeholder="请输入预留" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="materialruleconfig">
import { listMaterialRuleConfig,
 addMaterialRuleConfig, delMaterialRuleConfig, 
 updateMaterialRuleConfig,getMaterialRuleConfig, 
 } 
from '@/api/business/materialruleconfig.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
})
const columns = ref([
  { visible: true, prop: 'id', label: '序号' },
  { visible: true, prop: 'lineCode', label: '产线代号' },
  { visible: true, prop: 'stationCode', label: '工位代号' },
  { visible: true, prop: 'materialName', label: '物料名称' },
  { visible: true, prop: 'materialCode', label: '物料号' },
  { visible: true, prop: 'drawingNumber', label: '零件图号' },
  { visible: true, prop: 'orderCode', label: '订单号' },
  { visible: true, prop: 'productType', label: '产品类型' },
  { visible: false, prop: 'startIndex', label: '起始索引（用于解析规则）' },
  { visible: false, prop: 'endIndex', label: '结束索引（用于解析规则）' },
  { visible: false, prop: 'productionBatchNum', label: '生产批次号' },
  { visible: false, prop: 'supplierName', label: '供应商名称' },
  { visible: false, prop: 'totalCount', label: '总数' },
  { visible: false, prop: 'finishedCount', label: '消耗数量' },
  { visible: false, prop: 'noteInfo', label: '备注' },
  { visible: false, prop: 'bomVersion', label: 'BOM版本' },
  { visible: false, prop: 'creationTime', label: '创建时间' },
  { visible: false, prop: 'creatorName', label: '创建人' },
  { visible: false, prop: 'isEnabled', label: '软删除（1：启用，0：弃用）' },
  { visible: false, prop: 'spare1', label: '预留' },
  { visible: false, prop: 'spare2', label: '预留' },
  { visible: false, prop: 'spare3', label: '预留' },
  { visible: false, prop: 'spare4', label: '预留' },
  { visible: false, prop: 'spare5', label: '预留' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]


function getList(){
  loading.value = true
  listMaterialRuleConfig(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    id: [{ required: true, message: "序号不能为空", trigger: "blur"    , type: "number"  }],
  },
  options: {
    // 产品类型 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
productTypeOptions: [],
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel(){
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    lineCode: null,
    stationCode: null,
    materialName: null,
    materialCode: null,
    drawingNumber: null,
    orderCode: null,
    productType: null,
    startIndex: null,
    endIndex: null,
    productionBatchNum: null,
    supplierName: null,
    totalCount: null,
    finishedCount: null,
    noteInfo: null,
    bomVersion: null,
    creationTime: null,
    creatorName: null,
    isEnabled: null,
    spare1: null,
    spare2: null,
    spare3: null,
    spare4: null,
    spare5: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加 物料规则配置'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getMaterialRuleConfig(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改 物料规则配置'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateMaterialRuleConfig(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addMaterialRuleConfig(form.value).then((res) => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delMaterialRuleConfig(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}





handleQuery()
</script>